$.ajaxSetup({
	async : false
});

$(document).ready(function() {
	$("input[type='file']").change(function() {
		document.form1.path.value = this.value;
		var path_url = $("#path").val();
		if (isvalidatefile(path_url)) {
			previewImage(this);
		}
	});
	$("#upload_area").find("a").click(function() {
		document.form1.picpath.click();
	});

	$("#submit_button").find("a").click(function() {
		var path_url = $("#path").val();
		if (isvalidatefile(path_url)) {
			var options = {
				url : "adminmgr/img_updateHeaderImg",
				type : "POST",
				dataType : "text",
				success : function(data) {
					if (data == "2") {
						alert("更新成功");
						window.location.reload(true);
					} else if (data == "-2") {
						alert("更新失败");
					}  else if (data == "-5") {
						alert("请上传小于2M的图片");
					}else {
						alert("服务器异常，请稍后操作");
					}
				}
			};
			$("form:first").ajaxSubmit(options);
			return false;
		}
	});


	if (!+[ 1, ]) {
		$("#upload_area").find("a").hide();
		$("#upload_area").find("input[type='text']").hide();
		$("#picpath").css({
			width : "240px",
			height : "20px",
			filter : "alpha(opacity=100)"
		});
	}
});


/**
 * 验证上传文件的格式,大小
 * @param path_url 文件路径
 */
function isvalidatefile(path_url) {
	var pathvalue = path_url.substring(path_url.lastIndexOf(".") + 1);
	if (pathvalue == null || pathvalue == "") {
		alert("请选择图片");
		document.form1.path.value = "";
		return false;
	} else {
		if (!(pathvalue == "jpg" || pathvalue == "jpeg" || pathvalue == "JPG" || pathvalue == "JPEG")) {
			alert("请上传jpg/jpeg格式的图片");
			document.form1.path.value = "";
			return false;
		} else {
			//ie下js无法获得上传文件大小
			//所以ie下在前台不做图片大小验证，统一由服务端验证
			//这里只做非ie的验证
			if(navigator.userAgent.indexOf('MSIE') < 0) {
				var fileInput = $("input[type='file']")[0];
				var byteSize = fileInput.files[0].size;
				var imgSize = Math.ceil(byteSize / 1024);
				if (imgSize >= 2048) {
					alert("请上传小于2M的图片");
					document.form1.path.value = "";
					return false;
				} 
			}
		}
	}
	return true;
}

/**
 * 预览
 * 
 * @param file
 */
function previewImage(file) {
	var porImg = $('#biuuu');
	if (file["files"] && file["files"][0])
    {
        var reader = new FileReader();
        reader.onload = function(evt){
            porImg.attr({src : evt.target.result});
        };
       reader.readAsDataURL(file.files[0]);
    }else {
		var ieImageDom = document.createElement("div");
        var proIeImageDom = document.createElement("div");
        $(ieImageDom).css({
            float: 'left',
            position: 'relative',
            overflow: 'hidden',
            width: '100px',
            height: '100px'
        }).attr({"id":"view"});
        $(proIeImageDom).attr({"id":"biuuu"});
        porImg.parent().prepend(proIeImageDom);
        porImg.remove();
        file.select();
        path = document.selection.createRange().text;
        $(ieImageDom).css({"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")"});
        $(proIeImageDom).css({"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='scale',src=\"" + path + "\")"});
	}
}
